/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                        *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
package org.adempiere.model;

import java.sql.*;
import java.util.*;
import org.compiere.util.DB;
import org.jfree.util.Log;


/**
 * 	Barcode Trx Line
 *
 *  @author Shindu
 *  @since 1 May 2013
 */
public class MBarcodeTrxLine extends X_M_BarcodeTrxLine
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1212241234L;
	
	/**
	 * Update M_InOutLine_ID
	 * @param ctx
	 * @param M_BarcodeTrx_ID 
	 * @param M_Product_ID
	 * @param C_UOM_ID
	 * @param C_OrderLine_ID, optional. Jika 0 dianggap tidak di pakai
	 * @return
	 */
	public static boolean updateMInoutLineID(String trxName, int M_InOutLine_ID,
			int M_BarcodeTrx_ID, int M_Product_ID, int C_UOM_ID, int C_OrderLine_ID){
		boolean result;		
		String sql = "UPDATE M_BarcodeTrxLine " +
					"SET M_InOutLine_ID=?, DocStatus = '" + DOCACTION_Complete + "' " +
					"WHERE M_BarcodeTrx_ID=? " +
					"AND M_Product_Id=? " +
					"AND C_UOM_ID=? ";
		if (C_OrderLine_ID!=0)
			sql = sql + "AND C_OrderLine_ID= ? ";
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		try
		{
			pstmt = DB.prepareStatement(sql, trxName);
			pstmt.setInt(1, M_InOutLine_ID);
			pstmt.setInt(2, M_BarcodeTrx_ID);
			pstmt.setInt(3, M_Product_ID);
			pstmt.setInt(4, C_UOM_ID);
			if (C_OrderLine_ID!=0)
				pstmt.setInt(5, C_OrderLine_ID);
			pstmt.execute();
			result=true;
			//		
		}
		catch (SQLException ex)
		{		
			System.out.println("Could not update Lines:" + ex.getMessage());			
			result=false;			
			//throw new IllegalStateException("Could not update Lines: "+ ex.getMessage());
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		return result;
		
	}
	
	/**
	 * Get last Barcode Transaction Line filter by OrderLineID and BarcodeTrxID
	 * @param ctx
	 * @param M_BarcodeTrx_ID
	 * @param C_OrderLine_ID
	 * @param trxName
	 * @return
	 */
	public static MBarcodeTrxLine getLastLine(Properties ctx, int M_BarcodeTrx_ID, int C_OrderLine_ID, String trxName)
	{
		String sql="SELECT * FROM M_BarcodeTrxLine " +
				"WHERE C_OrderLine_ID=? AND M_BarcodeTrx_ID=? " +
				" ORDER BY M_BarcodeTrxLine_ID DESC ";
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		MBarcodeTrxLine result=null;
		try
		{
			pstmt = DB.prepareStatement(sql, trxName);
			pstmt.setInt(1, C_OrderLine_ID);
			pstmt.setInt(2, M_BarcodeTrx_ID);	
			rs = pstmt.executeQuery();
			if (rs.next()){
				result = new MBarcodeTrxLine(ctx, rs, trxName) ;
			}
		}
		catch (SQLException ex)
		{
			Log.error(ex.getMessage());
			result=null;
		}
		finally
		{
			DB.close(rs, pstmt);
		}		
		return result;		
	}
	
	/***
	 * Standard constructor
	 * @param ctx
	 * @param M_BarcodeTrxLine_ID
	 * @param trxName
	 */
	public MBarcodeTrxLine (Properties ctx, int M_BarcodeTrxLine_ID, String trxName)
	{
		super (ctx, M_BarcodeTrxLine_ID, trxName);
	}
	
	
	/**************************************************************************
	 * 	Standard Constructor
	 *	@param ctx context
	 *	@param M_BarcodeTrxLine_ID id
	 *  @param M_BarcodeTrx_ID master id
	 *	@param trxName trx name
	 */
	public MBarcodeTrxLine (Properties ctx, int M_BarcodeTrxLine_ID, int M_BarcodeTrx_ID, String trxName)
	{
		this (ctx, M_BarcodeTrxLine_ID, trxName);
		setM_BarcodeTrx_ID(M_BarcodeTrx_ID);
		//default status=Draft
		setDocStatus(DOCACTION_Draft);
	}	

	/**
	 *  Load Constructor
	 *  @param ctx context
	 *  @param rs result set record
	 *  @param trxName transaction
	 */
	public MBarcodeTrxLine (Properties ctx, ResultSet rs, String trxName)
	{
		super(ctx, rs, trxName);
	}	//	

	/**************************************************************************
	 * 	Before Save
	 *	@param newRecord new
	 *	@return save
	 */
	protected boolean beforeSave (boolean newRecord)
	{
		log.fine("");		
		return true;
	}	//	beforeSave
	
	/**
	 * 	Before Delete
	 *	@return true if drafted
	 */
	protected boolean beforeDelete ()
	{
		return true;
	}	//	beforeDelete

	/**
	 * 	String Representation
	 *	@return info
	 */
	public String toString ()
	{
		StringBuffer sb = new StringBuffer ("MBarcodeTrxLine[").append (get_ID())
			.append(",M_Inoutline_ID=").append(getM_InOutLine_ID())
			.append(",Qry=").append(getQty())
			.append ("]");
		return sb.toString ();
	}	//	toString

		
	protected boolean afterSave (boolean newRecord, boolean success)
	{
		return true;
	}	//	afterSave	
    /*** 
	 *   Add overrides funciton after delete
	 */
	protected boolean afterDelete (boolean success)
	{
		return true;
	}	//	afterDelet
	
	
}	
